﻿
using System;

namespace LxCFD
{
    public class TransientTimeBase
    {
        public double ResultTime;//对于显示格式，Result的时间在离散方程的下一个，对于隐式是一样的，ResultTime即为t时间，对于的时间间隔为delta_t

        public bool IsImplicit = true;
        public bool IsSecondOrder = true;

        public double EquationTime;//离散方程对应的时间，在每次调用isContinue时，要准备好下一次计算的equationTime
        public double dt;
        public double dto;
        public double dtoo;

        public virtual void Initialize()
        {
        }
        public virtual void MoveNextTimeStep()
        {

        }

        
        public virtual TransientTimeBase GetCopy()
        {
            throw new NotImplementedException();
        }
        public void SetTransientFactor()
        {
            TransientDataBase.IsSecondOrder = IsSecondOrder;
            if (IsImplicit)
            {
                GetFactor_1stOrderImplicit(out TransientDataBase.Factor_Phi_1stOrder);
                GetFactor_2ndOrderImplicit(out TransientDataBase.Factor_Phi_2ndOrder, out TransientDataBase.Factor_PhiO_2ndOrder, out TransientDataBase.Factor_PhiOO_2ndOrder);
            }
            else
            {
                GetFactor_1stOrderExplicit(out TransientDataBase.Factor_Phi_1stOrder);
                GetFactor_2ndOrderExplicit(out TransientDataBase.Factor_Phi_2ndOrder, out TransientDataBase.Factor_PhiO_2ndOrder, out TransientDataBase.Factor_PhiOO_2ndOrder);
            }
        }
        protected void GetFactor_1stOrderExplicit(out double beforePhi)
        {
            beforePhi = 1.0 / dto;
        }
        protected void GetFactor_2ndOrderExplicit(out double beforePhi, out double beforePhiOld, out double beforePhiOld2)
        {
            beforePhiOld2 = (dt + 2 * dto) * dto / ((dt + 2 * dto + dtoo) * (dto + dtoo));
            beforePhiOld = (dt + 2 * dto) * dtoo / ((dt + dto) * (dto + dtoo));
            beforePhi = 1 - beforePhiOld - beforePhiOld2;
            beforePhi = dto / (dt + dto) - beforePhi;
            beforePhiOld = dt / (dt + dto) - beforePhiOld;
            beforePhi /= dto;
            beforePhiOld /= dto;
            beforePhiOld2 /= -dto;
        }
        protected void GetFactor_1stOrderImplicit(out double beforePhi)
        {
            beforePhi = 1.0 / dt;
        }
        protected void GetFactor_2ndOrderImplicit(out double beforePhi, out double beforePhiOld, out double beforePhiOld2)
        {
            beforePhi = 1.0 / dt + 1.0 / (dt + dto);
            beforePhiOld2 = dto / (dt * (dto + dtoo));
            beforePhiOld = -(beforePhi + beforePhiOld2);
        }
    }
}
